
local module = _G["BlinkHealthTextModule"]

if select(2, UnitClass("player")) ~= "WARLOCK" or not module then return end

local loaded = false
local L_USE_POWER = "Use AltPower"
if GetLocale()=="koKR" then
L_USE_POWER = "2차 자원 표시하기"
end

local text
local name, rank, icon, count, debufType, duration, expirationTime
local size
local auraCount
local usable, nomana
local aCount
local updateAlterPower

local function getSize(baseSize, expirationTime, duration)
	local size = (expirationTime-GetTime()) / duration
	if size < 0.3 then
		size = baseSize * .8
	elseif size < 0.6 then
		size = baseSize * 1.0
	else
		size = baseSize * 1.2
	end
	return size / 2
end

module.config = {
}

function module:init()
	self.addon.mainFrame:RegisterEvent("PLAYER_ALIVE")
	self.addon.mainFrame:RegisterEvent("LEARNED_SPELL_IN_TAB")
	self.addon.mainFrame:RegisterEvent("CHARACTER_POINTS_CHANGED")

	self:update()

	if loaded==false then
		self:AddMiscConfig{
			type = "toggle",
			text = L_USE_POWER,
			tooltip = L_USE_POWER,
			get = function ()
				return module.addon.db.wl_use_power
			end,
			set = function (value)
				module.addon.db.wl_use_power = value
				if module.addon.db.wl_use_power and (module.primary == SPEC_WARLOCK_DEMONOLOGY or module.primary == SPEC_WARLOCK_DESTRUCTION) then
					module:EnablePower()
				else
					module:DisablePower()
				end
			end,
			disable = not module.addon.db.useModule,
		}

	end
	loaded = true
end

function module:update()
	self.primary = GetSpecialization()
	for auraName, t in pairs(self.config) do
		if t.talent and t.talent > 0 then
			local name, iconTexture, tier, column, selected, available = GetTalentInfo(t.talent);
			if name and available then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		else
			self.config[auraName].use = false
			if IsPlayerSpell(t.spellid) then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		end
	end
	if self.addon.db.wl_use_power and (self.primary == SPEC_WARLOCK_DEMONOLOGY or self.primary == SPEC_WARLOCK_DESTRUCTION) then
		self:EnablePower()
	else
		self:DisablePower()
	end

end

function module:LEARNED_SPELL_IN_TAB(...)
	self:update()
end
module.CHARACTER_POINTS_CHANGED = module.LEARNED_SPELL_IN_TAB
function module:PLAYER_ALIVE(...)
	self.addon.mainFrame:UnregisterEvent("PLAYER_ALIVE")
	self:update()
end

function module:EnablePower()
	if not self.addon.playerFrame.altPower then
		self.addon.playerFrame.altPower = self.addon.playerFrame:CreateFontString(nil, "OVERLAY")
		self.addon.playerFrame.altPower:SetFont(self.addon.db.font, self.addon.db.fontSizePower*0.75, self.addon.db.fontOutline)
		self.addon.playerFrame.altPower:SetPoint("BOTTOM", self.addon.playerFrame.health, "TOP")
		self.addon.playerFrame.altPower:SetAlpha(self.addon.db.unit.player.alpha)
		self.addon.playerFrame.altPower:SetJustifyH("CENTER")
	end
	self.addon.playerFrame.altPower:Show()
	self:RegisterUpdators(updateAlterPower)
end

function module:DisablePower()
	if self.addon.playerFrame.altPower then
		self.addon.playerFrame.altPower:Hide()
	end
	self:UnregisterUpdators(updateAlterPower)
end

function updateAlterPower()
	local alt_power = 0
	if module.primary == SPEC_WARLOCK_AFFLICTION then
		if ( IsPlayerSpell(WARLOCK_SOULBURN) ) then
			alt_power = SPELL_POWER_SOUL_SHARDS
		end
	elseif module.primary == SPEC_WARLOCK_DEMONOLOGY then
		alt_power = SPELL_POWER_DEMONIC_FURY
	elseif module.primary == SPEC_WARLOCK_DESTRUCTION then
		if ( IsPlayerSpell(WARLOCK_BURNING_EMBERS) ) then
			alt_power = SPELL_POWER_BURNING_EMBERS
		end
	end
	if alt_power > 0 then
		local df = UnitPower("player", alt_power)
		local text = ""
		text = ("|cff%02x%02x%02x%d|r"):format(127, 82, 127, df)
		module.addon.playerFrame.altPower:SetText(text)
	else
		module.addon.playerFrame.altPower:SetText("")
	end
end

function module:getPlayerText()
	if self.addon.db.wl_use_power and self.primary == SPEC_WARLOCK_AFFLICTION then
		if ( IsPlayerSpell(WARLOCK_SOULBURN) ) then
			local shards = UnitPower( "player", SPELL_POWER_SOUL_SHARDS )
			if shards > 0 then
				return (":|cff%02x%02x%02x%d|r"):format(127, 82, 127, shards)
			end
		end
	end
	return ""
end
